/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.BookinfoDao;
import db.JDBCUtil;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jim
 */
public class bookInfoDaoImpl implements BookinfoDao {
      public static void main(String[] args){
        List<bookInfo> list=new bookInfoDaoImpl().displayAllbookInfo();
     
    }

    @Override
    public List<bookInfo> displayAllbookInfo() {
          Connection conn=null;
        Statement stat=null;
        ResultSet rs=null;
        String sql="select * from booksinfo";
        List<bookInfo> list=new ArrayList<bookInfo>();
           try{
            conn=JDBCUtil.getConnection();
            stat=conn.createStatement();
            rs=stat.executeQuery(sql);
            while(rs.next()){
                bookInfo book=new bookInfo();
                book.setBookName(rs.getString("bookName"));
                book.setCount(rs.getInt("count"));
                book.setPrices(rs.getInt("prices"));
                list.add(book);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtil.realeaseAll(rs, stat, conn);
        }
        return list;
    }

    @Override
    public boolean writebook(bookInfo book) {
        Connection conn = null;
		PreparedStatement pstat = null;
		String sql = ("insert into booksinfo(bookName,count,prices)"+ "values(?,?,?)");
		int result = -1;
		try {
			conn = JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, book.getBookName());
			pstat.setInt(2, book.getCount());
			pstat.setInt(3, book.getPrices());
			result = pstat.executeUpdate();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.realeaseAll(null, pstat, conn);
		}
		return result > 0 ? true : false;
    }

    @Override
    public bookInfo readbook(String bookname) {
         Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from booksinfo where bookName like ?";
		bookInfo book=null;
		try {
			conn = JDBCUtil.getConnection();
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, bookname);
			rs = pstat.executeQuery();
			while (rs.next()) {
				
				String bookName = rs.getString("bookName");
				int bookcount = rs.getInt("count");
				int bookprices =rs.getInt("prices");
				book = new bookInfo();
                                book.setBookName(bookName);
				book.setCount(bookcount);
				book.setPrices(bookprices);
                                System.out.println(book.getBookName());
			}
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.realeaseAll(rs, pstat, conn);
		}
                
		return book;
    }

    @Override
    public boolean delete(String bookName) {
       Connection conn = null;
        PreparedStatement pstmt = null;
		int n = 0;
		String sql = "delete from booksinfo where bookName = ?";
		try {
			conn = JDBCUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, bookName);
			n = pstmt.executeUpdate();
			System.out.println("删除语句执行结果-----" + n);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.realeaseAll(null, pstmt, conn);
		}
		return n > 0 ? true : false;
    }

    @Override
    public boolean update(bookInfo oldbook,bookInfo newbook) {
      Connection conn = null;
		// 创建预处理命令对象
		PreparedStatement pstmt = null;
		int n = 0;
		try {
			conn = JDBCUtil.getConnection();
			pstmt = conn
					.prepareStatement("update booksinfo set bookName=?,count=?,prices=? where bookName = ?");
			pstmt.setString(1, newbook.getBookName());
			pstmt.setInt(2, newbook.getCount());
			pstmt.setInt(3, newbook.getPrices());
                        pstmt.setString(4, oldbook.getBookName());
			n = pstmt.executeUpdate();
			System.out.println("插入语句执行结果-----" + n);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.realeaseAll(null, pstmt, conn);
		}
		return n > 0 ? true : false;
	}
    }


  
   

